﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace Cips.DataMining.IO
{
	public static class CsvDictionary
	{
		public static Dictionary<string, string> Read(Stream inputStream)
		{
			using (var reader = new CsvReader(inputStream))
				return reader.ReadToEnd().Where(l => l.Length == 2).ToDictionary(l => l[0], l => l[1]);
		}

		public static void Write(Stream outputStream, Dictionary<string, string> dictionary)
		{
			using(var writer = new StreamWriter(outputStream))
				foreach (var pair in dictionary)
					writer.WriteLine("{0},{1}", pair.Key.ToCsvString(), pair.Value.ToCsvString());
		}

		static string ToCsvString(this string input)
		{
			return '"' + input.Replace("\"", "\"\"") + '"';
		}
	}
}
